//
//  ViewController.m
//  20-XML-SAXAndDOM
//
//  Created by Snow on 16/8/10.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domAtuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}
//xml分为2种解析方式
//第一种 SAX-逐行解析 -- 使用系统的解析器NSXMLParser
- (IBAction)SAXParser:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}
//1解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _stuArray = [NSMutableArray array];
}
//2遇到结点头 调用的方法 / 方法的回调 记录当前节点, 方便后续赋值
//打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    _currentElementName = elementName;
    NSLog(@"%@", elementName);
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [[Student alloc] init];
        [_stuArray addObject:stu];
    }
    NSLog(@"结点头%@", elementName);
}
//3节点值 根据节点决定值 没有值时 对应的结果\n\t  \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (![string hasPrefix:@" "] && ![string hasSuffix:@"\n"]) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
    }
    NSLog(@"节点的值--结束的值%@", string);
}
//4遇到节点尾 调用的方法/方法的回调
- (void)parser:(NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
{
    NSLog(@"节点尾%@", elementName);
}
//5解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser
{}
//6解析失败时 调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"解析失败:%@", parseError);
}

//第二种解析方式 DOM解析
//关于GDataXMLNode的引入
//1.引入.h .m
//2.引入系统libxml2.tdb
//3.根据XML头文件中的提示 添加 /user/include/libxml2
//4.如果是ARC环境 需对该文件进行MRC编译 添加 -fno-objc-arc
- (IBAction)DOMParse:(id)sender {
    _domAtuArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    //1根据XML的data数据 生成一个树形结构的document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    //2根据document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    NSLog(@"%@", rootElement.name);
    //3根据根节点 获取子节点数
    NSArray *rootChildren = rootElement.children;
    //4遍历子节点数
    for (GDataXMLElement *childElement in rootChildren) {
        NSLog(@"childElement name : %@", childElement.name);
        //5.对应所有student节点
        Student *stu = [Student new];
        //6遍历student节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            //7.赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@", _domAtuArray);
}
//XMLReader
//将 xml 转换成dictionary

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
